package cn.com.higinet.dl.joa.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.higinet.dl.joa.service.ForumService;
import cn.com.higinet.dl.joa.service.UserService;
import cn.com.higinet.dl.joa.view.Model;
@Controller("forumController")
public class ForumController {
	@Autowired
	private ForumService forumService;
	
	public ForumService getForumService(){
		return forumService;
	}
	
	public void setForumService(ForumService forumService){
		this.forumService = forumService;
	}
	@RequestMapping(value = "/release", method = RequestMethod.GET)
	public String releaseView(HttpSession session) {
		
		if( (session.getAttribute("userName").toString()).equals(null))
		    return "user/login";
		else
			return "user/...";
	}
	@RequestMapping(value = "/...", method = RequestMethod.GET)
	public String topicView(HttpSession session) {
		if(null != session.getAttribute("userName"))
		    return "user/...";
		else
			return "user/login";
	}
	@RequestMapping(value = "/createtopic", method = RequestMethod.GET)
	public String createView(HttpSession session) {
		
		if( (session.getAttribute("userName").toString()).equals(null))
		    return "user/login";
		else
			return "user/...";
	}
	@RequestMapping(value="/release",method=RequestMethod.POST)
	public Model releasePost(@RequestParam Map<String, Object> map,HttpSession session) throws MessagingException{
	
	
		Model model=new Model();
		String postTitle=map.get("postTitle").toString();
		String postText=map.get("postText").toString();
		Map<String, Object> reqs = new HashMap<String, Object>();
		reqs.put("postTitle", postTitle);
		reqs.put("postText", postText);
		if(!postTitle.equals("")&&!postText.equals(""))
		{   
			model.set("result", "success");
			forumService.addPost(reqs);
			
		}
		else{
			model.set("result", "error");
		}
		return model;
	}
	@RequestMapping(value="/createTopic",method=RequestMethod.POST)
	public Model createTopic(@RequestParam Map<String, Object> map) throws MessagingException{
		Model model=new Model();
		String topicTitle=map.get("topicTitle").toString();
		String topicDesc=map.get("topicDesc").toString();
		Map<String, Object> reqs = new HashMap<String, Object>();
		reqs.put("topicTitle", topicTitle);
		reqs.put("topicDesc", topicDesc);
		if(!topicTitle.equals(""))
		{   
			model.set("result", "success");
			forumService.addTopic(reqs);
			
		}
		else{
			model.set("result", "error");
		}
		return model;
	}
	/*@RequestMapping(value="/。。。",method=RequestMethod.POST)
	public Model topic(@RequestParam Map<String, Object> map) throws MessagingException{
		Model model=new Model();
		
		model.setRow(forumService.getAllTopic());
		return model;
	}
	
	@RequestMapping(value="/。。",method=RequestMethod.POST)
	public Model digest(@RequestParam Map<String, Object> map) throws MessagingException{
		Model model=new Model();
		
		model.setRow(forumService.getAllDigest());
		return model;
	}*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value="/getAllPost", method=RequestMethod.POST)
	public void getAllPost(@RequestParam Map<String, Object> reqs, 
			HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
		//String userName = session.getAttribute("userName").toString();
		//int userType =  userService.getUserType(userService.getUserIdByUsername(userName).toString());
		Map pageInfo = new HashMap();  
        pageInfo.put("page", 1);  
        pageInfo.put("total", 5); 

		String jsonStr = getJsonString(forumService.getAllPost(), pageInfo);
		response.setContentType("html/txt");  
        response.setCharacterEncoding("utf-8");  
        response.setHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache, must-revalidate");  
        response.setHeader("Pragma", "no-cache");  
        response.getWriter().write(jsonStr); 
        response.getWriter().flush(); 
        response.getWriter().close();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })  
    private String getJsonString(List list, Map pageInfo) {  
		  List mapList = new ArrayList();  
	        for(int i = 0; i < list.size(); i++) {  
	        	Map<String, Object> map = (Map<String, Object>) list.get(i);
	        	
	            Map cellMap = new HashMap();  
	            cellMap.put("id", map.get("postId"));  
	            cellMap.put("cell", new Object[] {map.get("postTitle"), map.get("postText"),map.get("createTime")});     
	            mapList.add(cellMap);  
	        }  
	        pageInfo.put("rows", mapList);  
	        JSONObject object = new JSONObject();
	        object.putAll(pageInfo);
	        return object.toString();  
	}
//	@RequestMapping(value = "/getAllPost")
//	public Model getAllPost(@RequestParam Map<String, Object> reqs, HttpServletRequest request,HttpServletResponse response) {
//		Model model = new Model();
//		
//		
//		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
//		
//			list = forumService.getAllPost();
//	
//		List<Map<String, Object>> result_list = new ArrayList<Map<String,Object>>();
//		String postId=null,postTitle=null,postText=null;
//		
//		for (int i = 0; i < list.size(); i++) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			postId = list.get(i).get("postId").toString();
//			postTitle = list.get(i).get("postTitle").toString();
//			postText = list.get(i).get("postText").toString();
//		
//			map.put("postId", postId);
//			map.put("postTitle", postTitle);
//			map.put("postText", postText);
//		
//		
//			result_list.add(map);
//		}
//		model.setList(result_list);
//		return model;
//	}

}
